package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;

import common.Utils;

import model.DrawObject;

public class ViewText extends ViewObject
{

	
	public ViewText(DrawObject obj, Graphics g)
	{
		super(obj, g);
	}

	public ViewText(DrawObject obj, Graphics2D g2)
	{
		super(obj, g2);
	}


	@Override
	public void Draw(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		
		Color c = new Color(dObj.getRed(), dObj.getGreen(), dObj.getBlue());
		g2.setColor(c);
		
		g2.setFont( Utils.getFont(dObj) );
		
		g2.drawString( dObj.getText(), dObj.getX1(), dObj.getY1() );
		
		// For temporary testing purposes
//		g2.setColor( new Color(0,255,0) );
//		g2.setStroke( new BasicStroke(1) );
//		g2.drawLine(dObj.getX1(), dObj.getY1(), dObj.getX1(), dObj.getY1());
		
		super.Draw(g);
	}

	@Override
	public int pFuncX(double t)
	{
		return 0;
	}

	@Override
	public int pFuncY(double t)
	{
		return 0;
	}

	@Override
	public BoundBox getBounds(boolean padded)
	{
		Font font = Utils.getFont(dObj);
		FontMetrics f_met = this.g2.getFontMetrics( font );
		Rectangle2D bounds = f_met.getStringBounds(dObj.getText(), this.g2);
		
		bounds.setRect(	(double)dObj.getX1() + bounds.getX(), 
						(double)dObj.getY1() + bounds.getY() + f_met.getDescent(), 
						bounds.getWidth(), 
						bounds.getHeight() - f_met.getDescent() );
		
		BoundBox box = new BoundBox();
		box.setX( (int)bounds.getX() );
		box.setY( (int)bounds.getY() );
		box.setWidth( (int)bounds.getWidth() );
		box.setHeight( (int)bounds.getHeight() );
		
		return box;
	}

	@Override
	public boolean containsPoint(Point p)
	{	
		if ( !getBounds(false).contains(p) )
			return false;
		
		
		// Find the selected char and its index. Save the index for retrieval.
		int len = dObj.getText().toCharArray().length;
		int index = -1;
		for (int i=0; i<len; i++)
		{
			int wid = getSubStringWidth(dObj.getText(), i);
			
			if ( (dObj.getX1() + wid - 1) > p.x )
			{
				index = i;
				break;
			}
		}
		
		this.sel_char_index = index;
		
		return true;
	}
	
	private int getSubStringWidth(String str, int index )
	{
		Font font = Utils.getFont(dObj);
		FontMetrics f_met = this.g2.getFontMetrics( font );
		
		char [] charList = dObj.getText().toCharArray();
		
		if (str.length() > 0)
			return f_met.charsWidth(charList, 0, index + 1);
		else
			return f_met.stringWidth(str);
	}
	
	@Override
	public void DrawControlPoints(Graphics g) 
	{
		this.DrawControlPoint(dObj.getX1(), dObj.getY1(), g);
		this.DrawCursor(g);
	}
	
	private void DrawCursor(Graphics g)
	{
		if (sel_char_index < 0)
			return;
		
		BoundBox box = this.getBounds(false);
		
		int width = getSubStringWidth(dObj.getText(), this.sel_char_index);
		
		int x1 = box.getX() + width -1;
		int x2 = x1;
		int y1 = box.getY();
		int y2 = y1 + box.getHeight() -1;
		
		Graphics2D g2 = (Graphics2D)g;
		g2.setColor( new Color(0,0,0) );
		g2.setStroke( new BasicStroke(1) );
		g2.drawLine(x1, y1, x2, y2);
	}
	
	@Override
	public int calcControlPoint(Point a) 
	{
		Point b = new Point(dObj.getX1(), dObj.getY1());
		if ( distSq(a, b) <= Utils.MIN_DIST_SQ )
			return 1;
		
		return 0;
	}
	
}
